<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<link rel="stylesheet" type="text/css" media="screen" href="css/redmond/jquery-ui-1.10.3.custom.min.css" />	<!-- jqueryUI CSS -->
<link rel="stylesheet" type="text/css" media="screen" href="css/selfTemplate.css" />						<!-- 本專案自行定義 CSS -->

<script src="js/jquery-2.1.0.min.js"></script>		<!-- jQuery 核心 -->
<script src="js/jquery-ui-1.10.3.custom.js" type="text/javascript"></script>							<!-- jqueryUI 核心 -->
<script src="js/jquery.cookie.js"></script>																	<!-- 操作cookie用 -->
<script src="js/widgets.js" type="text/javascript"></script>												<!-- 本專案自行定義 JS -->

<title>維護人員</title>

<script type="text/javascript">

function chkClick(empId,envId) {

	var systemId = $("select#systemId  option:selected").val();	//系統
	var subsysId = $("select#subsysId  option:selected").val();	//子系統
	var roleId = $("select#role  option:selected").val();		//角色
	
	var addOrDelete = "delete";
	if ($("input.empIn[value='"+envId+"']").is(":checked")) {
		addOrDelete = "add";
	}

	$.ajax({type:"post",url:"UpdateEmpRole",dataType: "json",
		data : {subsysId:subsysId,polyType:2,envId:envId,systemId:systemId,roleId:roleId,creatorId:$.cookie("empId"),addOrDelete:addOrDelete,empId:empId},
		success : function(reses) { if (reses != "") { $("#errShortMsg").html(reses); } }
	});
}

function assignEnv(empId) { 
	
	var systemId = $("select#systemId  option:selected").val();	//系統
	var subsysId = $("select#subsysId  option:selected").val();	//子系統
	var roleId = $("select#role  option:selected").val();		//角色
	
	$.ajax({type:"post",url:"QueryEnv",async:false,dataType:"json",
		data:{subsysId:'fOrder',fc:'selEnvBySubSys'},
		success : function(reses) {
			if ( reses.length > 0 ) {
				$("#tbodyAssignEnv tr").remove();
				
				//建立所有環境
				$.each(reses,function(i, res) {
					var idx = $("#tbodyAssignEnv tr").size()+1;
					var tmp = "<tr><td class='center'><input class='empIn' onclick=\"chkClick('"+empId+"','"+res.envId+"')\" type='checkbox' value='"+res.envId+"'/></td><td class='int chkRow'>"+idx+"</td><td class='left zhTwN chkRow'>"+res.envName+" ("+res.envId+")</td></tr>";
					$("#tbodyAssignEnv").append(tmp);
				});
				//綁定輔助點選
				$(".chkRow").bind("click",function(){
					$(this).parent("tr").children("td:first").children("input[type='checkbox']").click();
				});
				//檢查一下，如果所有子項都有勾，那chkAll也要勾
				chk("tableAssignEnvChk");
				
				refreshUI(1,"tbodyAssignEnv",false);	
				
				//為已經有的權限所打勾
				$.ajax({type:"post",url:"QueryEmpInEnv",dataType: "json",
					data : { systemId:systemId,subsysId:subsysId,roleId:roleId,empId:empId },
					success : function(reses) {
						if( reses.length > 0 ) {
							$.each(reses,function(i,res){
								$("input.empIn[value='"+res+"']").attr('checked', true);
							});
						}
					},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
				});
				
				$("#tableAssignEnvChk").prop("title","選擇相關環境").css("visibility", "visible").dialog({
					modal:true,show:"fold",hide:"fold",width:250, async:false,
					buttons : {
						"關閉" : function() {
							$(this).dialog("close");
						}
					}
				});
				
			} else {
				alert("未設定任何環境");
			}
		}, error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
	});
	
	
		
}

$(document).ready(function() {
	init("roleAssign.html");
	
	var t = querySubsys( $.cookie("empId"),$.cookie("pageName"),"subsysId",$.cookie("localSystemId"));

	if( t==1 ) {
		windowFreeze("畫面初始中");
		
		//取得系統清單
		$.ajax({type:"post",url:"QuerySysList",async:false,dataType:"json",
			success : function(reses) {
				if ( reses.length > 0 ) {
					$("select#systemId option").remove();
				
					$.each(reses,function(i, res) {
						var lab = "";
						if ( res.s2 == 't') {
							lab = " (本機)";
						}
						tmp = "<option isLocalSys='"+res.s2+"' value='"+res.i1+"'>"+res.s1+lab+"</option>";
						$("select#systemId").append(tmp);
					
						if ( res.s2 == 't') {
							$("select#systemId").val(res.i1);	 //預設選擇本機系統
						}
					});
				}
			}, error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
		});
		
		$.post("QueryRoleList", function(reses) {
			$("select#role option").remove();
			
			$("select#role").append("<option value='noselect' id='dOpt'>--請選擇--</option>");
			
			if ( $(reses).size() >0 ) {
				$.each(reses,function(i, res) {
					tmp = "<option value='"+res.id+"'>"+res.name+"</option>";
					$("select#role").append(tmp);
				});
			}
		});
		
		$("select#systemId").bind("change", function(){
			if( $("select#role #dOpt").size() == 0 ) {
				chgPeoList(1);
			}
		});
		
		$("select#subsysId").bind("change", function(){
			$("select#subsysId #dOpt").remove();
			chgPeoList(1);
		});

		$("select#role").bind("change", function(){
			$("select#role #dOpt").remove();
			chgPeoList(1);
		});
		//項目被點選，會影響按鈕能否被按下
		$("select#notInPeople").change(function(){
			chkButtonClickAble();
		});
		//項目被點選，會影響按鈕能否被按下
		$("select#inPeople").change(function(){
			chkButtonClickAble();
		});
		
		
		
		$("button.remove").bind("click", function() {
			/*
			1. 將選的加入 delList
			2. 加入 notInList
			3. 移出 inList
			*/
			var tmp = "";
			var subsysId = $("select#subsysId  option:selected").val();	//子系統
			var roleId = $("select#role  option:selected").val();		//角色
			var creatorId = $.cookie("empId");
			
			$("select#inPeople option"+$(this).val() ).each( function(){
				$.ajax({type:"post",url:"UpdateEmpRole",dataType: "json",
					data : {subsysId:subsysId,polyType:1,systemId:$(this).attr("sysSN"),roleId:roleId,creatorId:creatorId,addOrDelete:"delete",empId:$(this).val()},
					success : function(reses) { if (reses != "") { $("#errShortMsg").html(reses); } }
				});
				$("select#notInPeople").append("<option value='"+$(this).val()+"'>"+$(this).html()+"</option>");
				$(this).remove();
			});	
			chkButtonClickAble();
		});
		
		$("button.add").bind("click", function() {
			/*
			1. 將選的加入 addList
			2. 加入 inList
			3. 移出 notInList
			*/
			var tmp = "";
			var subsysId = $("select#subsysId  option:selected").val();	//子系統
			var roleId = $("select#role  option:selected").val();		//角色
			var creatorId = $.cookie("empId");
			
			$("#notInPeople option"+$(this).val() ).each( function(){
				$.ajax({type:"post",url:"UpdateEmpRole",dataType: "json",
					data : {subsysId:subsysId,polyType:1,systemId:$(this).attr("sysSN"),roleId:roleId,creatorId:creatorId,addOrDelete:"add",empId:$(this).val()},
					success : function(reses) { if (reses != "") { $("#errShortMsg").html(reses); } }
				});
				$("select#inPeople").append("<option ondblclick=\"assignEnv('"+$(this).val()+"')\" value='"+$(this).val()+"'>"+$(this).html()+"</option>");
				$(this).remove();
			});	
			chkButtonClickAble();
		});
		
		chkButtonClickAble();
		windowUnfreeze();
	}
	
	$("#isQueryCheck").button({icons:{ primary: "ui-icon-check" }});
	
}).tooltip();
</script>

</head>
<body>

<div id="header"></div>
<p>

<table id="parTable" >
	<thead class="tableFirst">
		<tr>
			<th class="right"><label for="systemId">系統：</label></th>
			<td class="left" colspan="2"><select class="zhTwN" id="systemId"><option value='noselect' id='dOpt'>--請選擇--</option></select></td>
		</tr>
		<tr>
			<th class="right"><label for="subsysId">子系統：</label></th>
			<td class="left" colspan="2"><select class="zhTwN" id="subsysId"><option value='noselect' id='dOpt'>--請選擇--</option></select></td>
		</tr>
		<tr>
			<th class="right"><label for="role">角色：</label></th>
			<td class="left"><select class="zhTwN" id="role"><option value='noselect' id='dOpt'>--請選擇--</option></select></td>
		</tr>
		<tr><td colspan="3">&nbsp;</td> </tr>
		<tr>
			<td rowspan="4">其它人員：<br><select class="zhTwN" id="notInPeople" multiple="multiple" size="5" ></select></td> 
			<td><button id="btnAddAll" value="" class="zhTwN add" type="button">全部加入</button></td>
			<td  rowspan="4">選取人員：<br><select class="zhTwN" id="inPeople" multiple="multiple" size="5" ></select></td>	
		</tr>
		<tr>
			<td><button id="btnAddSel" class="zhTwN add" value=":selected" type="button">選取加入</button></td>
		</tr>
		<tr>
			 <td><button id="btnRemoveSel" class="zhTwN remove" value=":selected" type="button">選取移除</button></td>
		</tr>
		<tr>
			<td><button id="btnRemoveAll" value="" class="zhTwN remove" type="button">全部移除</button></td>
		</tr>
	</thead>
</table>
</body>
<div class="dialog" id="dialogAssignEnv">
	<table id="tableAssignEnvChk">
		<thead class="tableFirst">
			<tr>
				<th>&nbsp;</th>
				<th>#</th>
				<th class="zhTwN">環境</th>
			</tr>
		</thead>
		<tbody id="tbodyAssignEnv"></tbody>
	</table>
</div>
</html>